package fun.sanguo.service.record.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import fun.sanguo.mapper.record.RecordRoleMapper;
import fun.sanguo.model.dto.PageInfo;
import fun.sanguo.model.dto.PageInsert;
import fun.sanguo.model.dto.PageQuery;
import fun.sanguo.model.dto.PageUpdate;
import fun.sanguo.model.dto.record.RecordRoleInsert;
import fun.sanguo.model.dto.record.RecordRoleParam;
import fun.sanguo.model.dto.record.RecordRoleUpdate;
import fun.sanguo.model.entity.record.RecordRoleEntity;
import fun.sanguo.model.enums.ResEnums;
import fun.sanguo.service.record.RecordRoleService;
import fun.sanguo.utils.DateUtils;
import fun.sanguo.utils.IdUtils;
import fun.sanguo.utils.exception.ArtificialException;

import javax.annotation.Resource;
import java.util.List;

/**
 * 内容管理-角色列表 Service实现类
 *
 * @author ZYP
 * @date 2023-04-25
 */
@Service
public class RecordRoleServiceImpl implements RecordRoleService {

    @Resource
    private RecordRoleMapper recordRoleMapper;

    /**
     * 查询页面table列表
     *
     * @param pageQuery
     * @return
     * @throws ArtificialException
     */
    @Override
    public PageInfo<RecordRoleEntity> getPage(PageQuery<RecordRoleParam> pageQuery) throws ArtificialException {
        // 查询参数
        RecordRoleEntity recordRoleEntity = new RecordRoleEntity();
        // 转换查询参数
        try {
            // 接收参数
            RecordRoleParam recordRoleParam = new ObjectMapper().convertValue(pageQuery.getParam(), RecordRoleParam.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordRoleParam, recordRoleEntity);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 查询数据并封装返回
        try {
            // 查询总量
            int total = recordRoleMapper.getTotal(recordRoleEntity);
            // 查询列表（分页）
            List<RecordRoleEntity> recordRoleEntityList = recordRoleMapper.getList(recordRoleEntity, pageQuery);
            // 封装并返回页面信息
            return new PageInfo<>(recordRoleEntityList, total, pageQuery);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 新增
     *
     * @param pageInsert
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean insert(PageInsert<RecordRoleInsert> pageInsert) throws ArtificialException {
        // 新增参数
        RecordRoleEntity recordRoleEntity = new RecordRoleEntity();
        // 转换新增参数
        try {
            // 接收参数
            RecordRoleInsert recordRoleInsert = new ObjectMapper().convertValue(pageInsert.getParam(), RecordRoleInsert.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordRoleInsert, recordRoleEntity);
            // 其他参数赋值
            recordRoleEntity.setId(String.valueOf(new IdUtils().nextId()));
            recordRoleEntity.setCreateBy("system");
            recordRoleEntity.setCreateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
            recordRoleEntity.setUpdateBy("system");
            recordRoleEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
            recordRoleEntity.setVersion(1);
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 新增数据并得到操作结果
        try {
            // 新增
            int amount = recordRoleMapper.insert(recordRoleEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 编辑
     *
     * @param pageUpdate
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean update(PageUpdate<RecordRoleUpdate> pageUpdate) throws ArtificialException {
        // 编辑参数
        RecordRoleEntity recordRoleEntity = new RecordRoleEntity();
        // 转换编辑参数
        try {
            // 接收参数
            RecordRoleUpdate recordRoleUpdate = new ObjectMapper().convertValue(pageUpdate.getParam(), RecordRoleUpdate.class);
            // 赋值到实体类
            BeanUtils.copyProperties(recordRoleUpdate, recordRoleEntity);
            // 其他参数赋值
            recordRoleEntity.setUpdateBy("system");
            recordRoleEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 编辑数据并得到操作结果
        try {
            // 编辑
            int amount = recordRoleMapper.update(recordRoleEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }

    /**
     * 删除
     *
     * @param pageUpdate
     * @return
     * @throws ArtificialException
     */
    @Override
    public boolean delete(PageUpdate<RecordRoleUpdate> pageUpdate) throws ArtificialException {
        // 删除参数
        RecordRoleEntity recordRoleEntity = new RecordRoleEntity();
        // 转换删除参数
        try {
            // 接收参数
            RecordRoleUpdate recordRoleUpdate = new ObjectMapper().convertValue(pageUpdate.getParam(), RecordRoleUpdate.class);
            // 赋值到实体类
            recordRoleEntity.setId(recordRoleUpdate.getId());
            // 其他参数赋值
            recordRoleEntity.setUpdateBy("system");
            recordRoleEntity.setUpdateTime(DateUtils.getTime(DateUtils.DATE_FORMAT_11));
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_PARAM.getCode(), ResEnums.ERR_PARAM.getMsg(), e);
        }
        // 删除数据并得到操作结果
        try {
            // 删除
            int amount = recordRoleMapper.delete(recordRoleEntity);
            // 返回操作结果
            return amount > 0;
        } catch (Exception e) {
            throw new ArtificialException(ResEnums.ERR_MYSQL.getCode(), ResEnums.ERR_MYSQL.getMsg(), e);
        }
    }
}
